﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

using PRET.DBAdapter;

namespace PRET.ReportSystem.Forms
{
    public partial class ConnectDBForm : Form
    {
        private BackgroundWorker backgroundDB;

        private string WinCCServerName = "";
        private string WinCCDBName = "Report_DB";
        private string WinCCUserID = "";
        private string WinCCPassWord = "";

        private bool isconnected = false;
        public bool IsConnected
        {
            get { return isconnected; }
        }

        public ConnectDBForm()
        {
            InitializeComponent();

            this.WinCCServerName = ConfigurationManager.ConnectionStrings["WinCCServerName"].ConnectionString;
            this.WinCCDBName = ConfigurationManager.ConnectionStrings["WinCCDBName"].ConnectionString;
            this.WinCCUserID = ConfigurationManager.ConnectionStrings["WinCCUserID"].ConnectionString;
            this.WinCCPassWord = ConfigurationManager.ConnectionStrings["WinCCPassWord"].ConnectionString;

            this.backgroundDB = new BackgroundWorker();
            this.backgroundDB.DoWork += backgroundDB_DoWork;
            this.backgroundDB.RunWorkerCompleted += backgroundDB_RunWorkerCompleted;
        }

        void backgroundDB_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.connectBtn.Enabled = true;
            this.exitBtn.Enabled = true;
            this.processLabel.Visible = false;
        }

        void backgroundDB_DoWork(object sender, DoWorkEventArgs e)
        {
            this.connectDB();
        }

        private void connectBtn_Click(object sender, EventArgs e)
        {
            this.connectBtn.Enabled = false;
            this.exitBtn.Enabled = false;
            this.processLabel.Visible = true;

            this.backgroundDB.RunWorkerAsync();
        }

        private void connectDB()
        {
            string msg = ServiceAdapter.ConnectDB(this.serverAddressTextBox.Text, this.dbTextBox.Text, this.passwordTextBox.Text, this.passwordTextBox.Text);
            if (msg == "")
            {
                MessageBox.Show("数据库连接成功，您现在可以查询生产报表。", "数据库连接成功");
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.ConnectionStrings.ConnectionStrings["WinCCServerName"].ConnectionString = this.serverAddressTextBox.Text;
                config.ConnectionStrings.ConnectionStrings["WinCCDBName"].ConnectionString = this.dbTextBox.Text;
                config.ConnectionStrings.ConnectionStrings["WinCCUserID"].ConnectionString = this.usernameTextBox.Text;
                config.ConnectionStrings.ConnectionStrings["WinCCPassWord"].ConnectionString = this.passwordTextBox.Text;
                this.isconnected = true;
                this.Close();
            }
            else
            {
                // For test
                this.isconnected = true;
                //MessageBox.Show(msg, "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ConnectDBForm_Load(object sender, EventArgs e)
        {
            this.serverAddressTextBox.Text = this.WinCCServerName;
            this.dbTextBox.Text = this.WinCCDBName;
            this.usernameTextBox.Text = this.WinCCUserID;
            this.passwordTextBox.Text = this.WinCCPassWord;
        }
    }
}
